<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />
	<title>通知栏监听</title>
	<!-- 使用px -->
	<link rel="stylesheet" href="js/ydui.px.css">
	<!-- 引入Vue2.x -->
	<script src="js/vue.min.js"></script>
	<!-- 引入组件库 -->
	<script src="js/ydui.px.js"></script>
	<!-- axios -->
	<script src="js/axios.min.js"></script>
	<body>
		<div id="app">
			<yd-layout>
				<yd-navbar slot="navbar" title="通知栏监听">
				</yd-navbar>
				<yd-cell-group>
					<yd-cell-item>
						<span slot="left" style="font-weight: bold">监听权限：</span>
						<!-- <span slot="right" @click="openAuth"  style="border: 1px solid blue;
						display: inline-block;
						padding: 5px 10px 5px 10px;
						border-radius: 5px;">按钮</span> -->
						<span slot="right">
							<yd-switch v-model="auth" disabled="true">
							</yd-switch>
						</span>
					</yd-cell-item>
					<yd-cell-item>
						<span slot="left" style="font-weight: bold">通知栏监听状态：</span>
						<span slot="right">
							<yd-switch v-model="listening" disabled="true">
							</yd-switch>
						</span>
					</yd-cell-item>
					<yd-cell-item>
						<span slot="left">回调地址：</span>
						<yd-input slot="right" v-model="callBackUrl"  placeholder="请输入回调地址"></yd-input>
					</yd-cell-item>
				</yd-cell-group>
				<div style="width:100%;height:20px;"></div>
				<yd-cell-group >
					<yd-cell-item>
						<span slot="left" style="font-weight: bold">包名：</span>
						<span slot="right">
							{{msg.packageName}}
						</span>
					</yd-cell-item>
					<yd-cell-item>
						<span slot="left" style="font-weight: bold">短语：</span>
						<span slot="right">
							{{msg.tickerText}}
						</span>
					</yd-cell-item>
					<yd-cell-item>
						<span slot="left" style="font-weight: bold">文字：</span>
						<span slot="right">
							{{msg.tickerText}}
						</span>
					</yd-cell-item>
					<yd-cell-item>
						<span slot="left" style="font-weight: bold">文本标题</span>
						<span slot="right">
							{{msg.title}}
						</span>
					</yd-cell-item>
				</yd-cell-group>
		
		
			</yd-layout>
		</div>
	</body>
	<script type="text/javascript">
		new Vue({
			el: '#app',
			data(){
				return{
					auth: false,
					listening: false,
					main:'',
					msg: {},
					callBackUrl:''

				}
			},
			created() {
				// 初始化
				this.init()
			},
			methods: {
				init(){
					var that = this
					document.addEventListener('plusready', function(){
						//  导入android类对象
						var Intent = plus.android.importClass("android.content.Intent");
						var context = plus.android.importClass("android.content.Context");//获取上下文
						var ComponentName = plus.android.importClass("android.content.ComponentName");
						var PackageManager = plus.android.importClass("android.content.pm.PackageManager");
						var NotificationManagerCompat = plus.android.importClass("android.support.v4.app.NotificationManagerCompat");
						var IntentFilter = plus.android.importClass('android.content.IntentFilter'); //引入过滤器
						var Intent = plus.android.importClass('android.content.Intent');
						var ActivityManager = plus.android.importClass("android.app.ActivityManager");
						plus.android.importClass("java.util.List");
						plus.android.importClass("java.util.Set");
						// 获取主Activity对象的实例
						that.main = plus.android.runtimeMainActivity();
						var filter = new IntentFilter();
						var intent = new Intent();
						//启动短信监听
						intent.setClassName(that.main, "com.cnupai.listen.server.WeChatNotificationListenerService");
						that.main.startService(intent);	

						//检测通知权限是否打开
						function isNotificationListenerServiceEnabled(){
							var packageNames = NotificationManagerCompat.getEnabledListenerPackages(that.main);
							if (packageNames.contains(that.main.getPackageName())) {
								console.log(1);
								that.auth = true;
							}else{
								console.log(0);
								that.auth = false;
								var Intent = plus.android.importClass('android.content.Intent');
								that.main.startActivity(new Intent("android.settings.ACTION_NOTIFICATION_LISTENER_SETTINGS"));
							}
						};
						//重新绑定 reBind
						var thisComponent = new ComponentName(that.main, "com.cnupai.listen.server.WeChatNotificationListenerService" )
						var pm = that.main.getPackageManager();
						pm.setComponentEnabledSetting(thisComponent, PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP);
        				pm.setComponentEnabledSetting(thisComponent, PackageManager.COMPONENT_ENABLED_STATE_ENABLED, PackageManager.DONT_KILL_APP);

						var receiver = plus.android.implements("io.dcloud.feature.internal.reflect.BroadcastReceiver",{
							onReceive:function(context,intent){
								plus.android.importClass(intent);
								//console.log(intent.getExtra("notificationInfo"));
								that.msg = JSON.parse(intent.getExtra("notificationInfo"));
								axios.post(that.callBackUrl, that.msg).then(res=>{
									console.log(res);
								}).catch(e=>{
									console.log(e);
								});
								//that.msgList =  [JSON.parse(intent.getExtra("notificationInfo"))].concat(that.msgList);
							}
						});	
						filter.addAction('location.reportsucc') //监听扫码广播
						that.main.registerReceiver(receiver, filter); //注册监听      
						//检测是否在运行
						function isServiceRunning(){
							var activityManager=that.main.getSystemService(context.ACTIVITY_SERVICE);
							that.listening = plus.android.invoke('com.cnupai.listen.server.Util', 'isServiceRunning', activityManager, 'com.cnupai.listen.server.WeChatNotificationListenerService');
							if(that.listening){
								console.log('监听中');
							}else{
								console.log('未监听，重新绑定');
								//that.main.startActivity(new Intent("android.settings.ACTION_NOTIFICATION_LISTENER_SETTINGS"));
								intent.setClassName(that.main, "com.cnupai.listen.server.WeChatNotificationListenerService");
								/** 退出Activity是，停止服务 */
								//main.stopService(intent);
								that.main.startService(intent);	
							}
						};
						setInterval(isServiceRunning, 3000);
						setInterval(isNotificationListenerServiceEnabled, 3000);
					});
				},
				openAuth(){
					var Intent = plus.android.importClass('android.content.Intent');
					this.main.startActivity(new Intent("android.settings.ACTION_NOTIFICATION_LISTENER_SETTINGS"));
				}
			}
		})
		
	</script>
	<style>
	
	</style>
</head>
<body>
	
</body>
</html>